<?php

namespace app\shop\model;
use app\BaseModel;
use think\facade\Cache;
//*------------------------------------------------------ */
//-- 物流信息
/*------------------------------------------------------ */
class ShippingLogModel extends BaseModel
{
    protected $table = 'shop_shipping_log';
    public  $pk = 'order_id';
    protected $mkey = 'shipping_log_list';

    public function getkuaidiInfo($orderInfo){

        $info = $this->where('order_id',$orderInfo['order_id'])->find();
        
        if (empty($info) == false){
            $info = $info->toArray();
        }
        
        if ($orderInfo['shipping_status'] == 1){

            // var_export($orderInfo);die;
            $info=$this->kuaidi100($orderInfo['shipping_code'],$orderInfo['invoice_no']);

            // $res = json_decode($reskuaidi100);
            // var_export($reskuaidi100);die;
            if (empty($info)== false){
                $this->where('order_id',$orderInfo['order_id'])->update(['data'=>json_encode($info['data'],JSON_UNESCAPED_UNICODE),'update_time'=>time()]);
            }else{
                $info['order_id'] = $orderInfo['order_id'];
                $info['data'] = $info['data'];
                $this->save(['order_id'=>$orderInfo['order_id'],'data'=>json_encode($info['data'],JSON_UNESCAPED_UNICODE),'update_time'=>time()]);
            }

        }else{
            $info['data'] = json_decode($info['data'],true);
        }
        Cache::set($this->mkey.$orderInfo['order_id'],$info,300);


        return $info;
    }


    //快递100---
    public function kuaidi100($postcom,$getNu){
        $post_data             = array();
        $post_data["customer"] = '724329334519F8F965374C7D0C513D8A';
        $key                   = 'gNgAxQsZ7412' ;
        $post_data["param"]    = '{"com":"' . $postcom . '","num":"' . $getNu . '"}';

        $url               ='http://poll.kuaidi100.com/poll/query.do';
        $post_data["sign"] = md5($post_data["param"] . $key . $post_data["customer"]);
        $post_data["sign"] = strtoupper($post_data["sign"]);

        $o = "";
        foreach ($post_data as $k=>$v){
            $o .= "$k=".urlencode($v) . "&";
        }
        $post_data = substr($o,0,-1);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        curl_close($ch);

        $data = str_replace("\"", '"', $result);
        $data = json_decode($data, true);
        return $data;
        
    }



}
